import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/execute_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';
import 'package:get/get.dart';

class WorkDetailController extends GetxController {
  ///当前作业类型
  String? ticketBigTypeName;

  ///作业申请节点
  ExecuteTicketReceiptList? ticketReceiptList;

  ///气体分析节点
  List<ExecuteTicketGasList>? ticketGasList;

  ///安全措施节点
  List<ExecuteWorkSafetyMeasuresList>? safetyMeasuresList;

  ///安全交底节点
  List<ExecuteTicketDiscloseList>? ticketDiscloseList;

  ///作业审批、关闭节点数据
  List<ExecuteWorkCheckList>? workCheckList;

  // 字典查出的作业类型
  List<GetDictAllEntity> workType = DictDataInfo.specialJobType;

  ///初始化数据
  Future<void> initDate(String ticketReceiptId) async {
    //获取数据-作业申请
    await getTicketReceiptDetail(ticketReceiptId);
    //获取数据-气体分析数据
    await getListGasInfoById(ticketReceiptId);
    //获取数据-安全措施数据
    await getListConfigByTicketReceiptId(ticketReceiptId);
    //获取数据-安全交底数据
    await getListDiscloseById(ticketReceiptId);
    //获取数据-作业审批、关闭节点数据
    await getListCheckById(ticketReceiptId);
  }

  ///获取数据-作业申请
  Future<void> getTicketReceiptDetail(String ticketReceiptId) async {
    //获取数据作业申请数据
    final res = await HttpUtil().generateGetRequest<ExecuteTicketReceiptList>(
        Interface.getTicketReceiptDetail,
        queryParameters: {'ticketReceiptId': ticketReceiptId}).execute();
    res.onSuccess((result) {
      ticketReceiptList = result.data;
      // 从字典中查找id相同的作业对象
      final item = workType.firstWhere((other) => ticketReceiptList?.ticketBigTypeId == other.id,
          orElse: GetDictAllEntity.new);
      ticketBigTypeName = item.dictionaryLabel;
    });
    update();
  }

  ///获取数据-气体分析
  Future<void> getListGasInfoById(String ticketReceiptId) async {
    //获取数据-气体分析数据
    final res = await HttpUtil().generateGetRequest<List<ExecuteTicketGasList>>(Interface.getListGasInfoById,
        queryParameters: {'ticketReceiptId': ticketReceiptId}).execute();
    res.onSuccess((result) {
      ticketGasList = result.data;
    });
    update();
  }

  ///获取数据-安全措施数据
  Future<void> getListConfigByTicketReceiptId(String ticketReceiptId) async {
    //获取数据-安全措施数据
    final res = await HttpUtil().generateGetRequest<List<ExecuteWorkSafetyMeasuresList>>(
        Interface.getListConfigByTicketReceiptId,
        queryParameters: {'ticketReceiptId': ticketReceiptId}).execute();
    res.onSuccess((result) {
      safetyMeasuresList = result.data;
    });
    update();
  }

  ///获取数据-安全交底数据
  Future<void> getListDiscloseById(String ticketReceiptId) async {
    //获取数据-安全交底数据
    final res = await HttpUtil().generateGetRequest<List<ExecuteTicketDiscloseList>>(
        Interface.getListDiscloseById,
        queryParameters: {'ticketReceiptId': ticketReceiptId}).execute();
    res.onSuccess((result) {
      ticketDiscloseList = result.data;
    });

    update();
  }

  ///获取数据-安全交底数据
  Future<void> getListCheckById(String ticketReceiptId) async {
    //获取数据-安全措施数据
    final res = await HttpUtil().generateGetRequest<List<ExecuteWorkCheckList>>(Interface.getListCheckById,
        queryParameters: {'ticketReceiptId': ticketReceiptId}).execute();
    res.onSuccess((result) {
      workCheckList = result.data;
    });

    update();
  }

  ///根据类型获取交底人员签名地址
  List<String> getDiscloseNameById(int type) {
    if (ticketDiscloseList != null) {
      for (var item in ticketDiscloseList!) {
        if (item.discloseType == type && item.discloseUserSign != null) {
          return item.discloseUserSign!;
        }
      }
    }
    return [];
  }
}

/// 测试用气体分析实体类-后面删除
class TestGasAnalysis {
  TestGasAnalysis(
      {this.time = '2022年10月11日10时20分', this.name = '可燃气体', this.value = '分析结果结果', this.analyst = '张欣欣'});

  final String time; //时间
  final String name; //代表气体
  final String value; //分析结果
  final String analyst; //分析人
}

/// 测试用安全措施实体类-后面删除
class TestSafetyMeasures {
  TestSafetyMeasures(
      {this.content = '1.动火设备内部构件清洗干净、蒸汽吹扫或水洗、置换合格、达到动火条件',
      this.picture = 'https://i0.hdslb.com/bfs/archive/3b7bb14252bb7247d58162018036bdf01b75d4e2.jpg',
      this.involve = 0,
      this.name = '张欣欣'});

  final String content; //安全措施内容
  final String picture; //图片
  final int involve; //是否涉及 0否 1是
  final String name; //确认人
}

///测试用安全交底实体类-后面删除
class TestSafetyDisclosure {
  TestSafetyDisclosure(
      {this.title = '作业负责人意见:',
      this.picture =
          'https://img1.baidu.com/it/u=2144678080,3669606650&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500',
      this.time = '2022年10月11日10时20分'});

  final String title; //安全措施内容
  final String picture; //图片
  final String time; //确认人
}
